package njust.dzh.ordersystem.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import njust.dzh.ordersystem.Bean.Food;
import njust.dzh.ordersystem.Adapter.FoodAdapter;
import njust.dzh.ordersystem.R;

public class HomeFragment extends Fragment {
    private Food[] foods = {new Food("牛肉汉堡", R.drawable.p1, "18", "4.7", "精选优质牛肉，搭配新鲜蔬菜和特制酱料，口感丰富，营养美味。"),
            new Food("巧克力蛋糕", R.drawable.p2, "15", "4.5", "采用进口巧克力制作，口感细腻，甜而不腻，是下午茶的绝佳选择。"),
            new Food("鲜虾培根披萨", R.drawable.p3, "58", "4.7", "新鲜大虾搭配香脆培根，芝士拉丝，口感丰富，是披萨爱好者的首选。"),
            new Food("奥尔良烤鸡翅", R.drawable.p4, "25", "4.6", "采用特制奥尔良腌料，外酥里嫩，香气四溢，让人回味无穷。"),
            new Food("鸡腿汉堡", R.drawable.p5, "38", "4.4", "选用优质鸡腿肉，搭配新鲜蔬菜和特制酱料，口感丰富，营养美味。"),
            new Food("鸡米花", R.drawable.p6, "12", "4.8", "外酥里嫩，口感香脆，是休闲小食的不二之选。"),
            new Food("肉酱意大利面", R.drawable.p7, "18", "4.9", "精选意大利面，搭配特制肉酱，口感丰富，让人回味无穷。"),
            new Food("火腿三明治", R.drawable.p8, "18", "4.5", "新鲜火腿搭配蔬菜，口感清爽，营养美味。"),
            new Food("蔬菜沙拉", R.drawable.p9, "16", "4.9", "精选新鲜蔬菜，搭配特制沙拉酱，健康美味。"),
            new Food("黑椒牛排", R.drawable.p10, "38", "4.7", "选用优质牛肉，搭配黑椒酱汁，口感丰富，让人回味无穷。"),
            new Food("蒜香大虾", R.drawable.p11, "28", "4.9", "新鲜大虾搭配蒜香酱汁，口感鲜美，让人回味无穷。"),
            new Food("焦糖玛奇朵", R.drawable.p12, "15", "4.6", "香浓咖啡搭配焦糖，口感丰富，是咖啡爱好者的首选。")
    };

    private List<Food> foodList = new ArrayList<>();
    private FoodAdapter foodAdapter;
    private SwipeRefreshLayout swipeRefresh;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        // 创建数据源
        initFoods();
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
        recyclerView.setLayoutManager(gridLayoutManager);
        // 创建适配器，同时加载数据源
        foodAdapter = new FoodAdapter(foodList);
        // 设置适配器
        recyclerView.setAdapter(foodAdapter);
        swipeRefresh = view.findViewById(R.id.swipe_refresh);
        swipeRefresh.setColorSchemeResources(R.color.design_default_color_primary);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshFoods();
            }
        });
        return view;
    }

    private void initFoods() {
        foodList.clear();
        Random random = new Random();
        int index = random.nextInt(foods.length);
        for (int i = index; i < foods.length; i++) {
            foodList.add(foods[i]);
        }
        for (int i = 0; i < index; i++) {
            foodList.add(foods[i]);
        }
    }

    private void refreshFoods() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        initFoods();
                        foodAdapter.notifyDataSetChanged();
                        swipeRefresh.setRefreshing(false);
                    }
                });
            }
        }).start();
    }

}
